{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data = pd.read_csv(\"csv_sample.csv\",index_col = 0)\n",
    "data.head()"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>height</th>\n",
       "      <th>weight</th>\n",
       "      <th>smoker</th>\n",
       "      <th>gender</th>\n",
       "      <th>age</th>\n",
       "      <th>color</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>163</td>\n",
       "      <td>56</td>\n",
       "      <td>True</td>\n",
       "      <td>男</td>\n",
       "      <td>23</td>\n",
       "      <td>yellow</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>189</td>\n",
       "      <td>67</td>\n",
       "      <td>True</td>\n",
       "      <td>女</td>\n",
       "      <td>58</td>\n",
       "      <td>yellow</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>165</td>\n",
       "      <td>53</td>\n",
       "      <td>False</td>\n",
       "      <td>男</td>\n",
       "      <td>42</td>\n",
       "      <td>black</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>168</td>\n",
       "      <td>88</td>\n",
       "      <td>False</td>\n",
       "      <td>女</td>\n",
       "      <td>79</td>\n",
       "      <td>yellow</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>177</td>\n",
       "      <td>89</td>\n",
       "      <td>False</td>\n",
       "      <td>女</td>\n",
       "      <td>29</td>\n",
       "      <td>yellow</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   height  weight  smoker gender  age   color\n",
       "0     163      56    True      男   23  yellow\n",
       "1     189      67    True      女   58  yellow\n",
       "2     165      53   False      男   42   black\n",
       "3     168      88   False      女   79  yellow\n",
       "4     177      89   False      女   29  yellow"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Pandas Serise 操作"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "#①使用字典进行映射\n",
    "data[\"gender\"] = data[\"gender\"].map({\"男\":1, \"女\":0})\n",
    "\n",
    "#②使用函数\n",
    "def gender_map(x):\n",
    "    gender = 1 if x == \"男\" else 0\n",
    "    return gender\n",
    "#注意这里传入的是函数名，不带括号\n",
    "data[\"gender\"] = data[\"gender\"].map(gender_map)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.9.6",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.6 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "ef233a7ddfbac837033f9c33ae60af661cf503add968c742fae8537839e961b7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}